package ofpof.domain.dao.mapper;

import java.util.Date;

import ofpof.domain.User;

import org.bson.types.ObjectId;
import org.springframework.stereotype.Component;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

@Component
public class UserMapper implements MongoMapper<User> {

	@Override
	public User fromDbObject(DBObject dbObject) {
		User user = new User();
		user.setEmail((String) dbObject.get("email"));
		user.setPassword((String) dbObject.get("password"));
		user.setCreatedOn((Date) dbObject.get("createdon"));
		user.setId((ObjectId) dbObject.get("_id"));
		user.setActive(Boolean.valueOf(dbObject.get("active").toString()));
		return user;
	}

	@Override
	public DBObject toDbObject(User entity) {
		DBObject dbObject  = new BasicDBObject();
		dbObject.put("email", entity.getEmail());
		dbObject.put("_id",  entity.getId());
		dbObject.put("password", entity.getPassword());
		dbObject.put("createdon",entity.getCreatedOn());
		dbObject.put("active", entity.isActive());
		return dbObject;
	}
}
